package com.sso.manager;

import com.sso.shiro.RealmManager;
import com.tools.util.StrUtils;
import com.sso.common.enums.SysResourceEnum;
import com.sso.dao.SysMenuResourceViewMapper;
import com.sso.entity.view.SysMenuResourceView;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.config.Ini;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Admin on 2016/1/22.
 */
@Service
public class SysMenuResourceViewManager {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SysMenuResourceViewMapper menuResourceViewDao;

    @Transactional(propagation= Propagation.SUPPORTS)
    public List<SysMenuResourceView> selectByProperty(Map<String,Object> filterMap){
        return menuResourceViewDao.selectByProperty(filterMap);
    }
    @Transactional(propagation= Propagation.SUPPORTS)
    public void getLoadSection(Ini.Section section) {
        Map<String, Object> filterMap = new HashMap<String, Object>();
        filterMap.put("resourceStatus", SysResourceEnum.status.START.key);
        //查询菜单与资源绑定视图
        List<SysMenuResourceView> menuResourceViewList = this.selectByProperty(filterMap);
        for (SysMenuResourceView menuResource : menuResourceViewList) {
            String auth="";
            //接口类型
            if(SysResourceEnum.type.WEB_SERVICE.key.equals(menuResource.getResourceType())){
                auth= "ws";

            //除接口以外的其它类型
            }else {
                if (SysResourceEnum.AuthType.ANON.key.equals(menuResource.getResourceAuthType())) {
                    auth = "anon";

                } else if (SysResourceEnum.AuthType.LOGIN.key.equals(menuResource.getResourceAuthType())) {
                    auth = "authc";
                } else {
                    String key = menuResource.getResourceUrl();
                    if (section.containsKey(key)) {
                        auth = "perms[" + section.get(key) + "," + menuResource.getMenuEname() + "]";
                    } else {
                        //设置权限校验key为菜单英文标识
                        auth = "perms[" + menuResource.getMenuEname() + "]";
                    }
                }
            }
            //装配权限
            logger.info("url="+menuResource.getResourceUrl()+"******权限="+auth);
            section.put(menuResource.getResourceUrl(), auth);
        }
    }

    /**
     * 加载用户的权限资源
     * @return
     */
    public List<SysMenuResourceView> loadUserResource(String userId){
        return menuResourceViewDao.loadUserResource(userId);
    }

    public SysMenuResourceView selectByResourceId(String resourceId){
        if(StrUtils.isEmpty(resourceId)){
            return new SysMenuResourceView();
        }
        Map<String,Object> filterMap = new HashMap<String, Object>();
        filterMap.put("resourceId",resourceId);
        List<SysMenuResourceView> menuResourceViewList= this.selectByProperty(filterMap);
        if(menuResourceViewList.size()==0 || menuResourceViewList.size()>1){
            logger.info("SysMenuResourceViewManager-->selectByResource-->查询错误 集合大小为:"+menuResourceViewList.size());
            return new SysMenuResourceView();
        }
        return menuResourceViewList.get(0);
    }
}
